﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZealousTTS.Common.Extensions;
using ZealousTTS.Domain;
using ZealousTTS.Web.ViewModels;
using ZealousTTS.Web.Utility;
using AutoMapper;

namespace ZealousTTS.Web.Areas.Private.Controllers
{
    public class StatusController : BaseController
    {
		private ZealousTTSEntities _Context;
        
		#region Constructors
		public StatusController()
		{
			_Context = new ZealousTTSEntities();
		}

		public StatusController(UserViewModel currentUser)
			: base(currentUser)
        {
			_Context = new ZealousTTSEntities();
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return View();
        }
		/// <summary>
        /// Gets a Status from Database to edit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
		public ActionResult Edit(int id = 0)
        {
            Status entity = id == 0 ? new Status() : _Context.Status.FirstOrDefault(P => P.ID == id);            
            StatusViewModel viewModel = Mapper.Map<Status, StatusViewModel>(entity);             
            return View(viewModel);
        }

		/// <summary>
        /// Saves Status Information into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
		[HttpPost]
		public ActionResult Edit(StatusViewModel viewModel)
        {
			if (!ModelState.IsValid)
                return View(viewModel);

			Status entity = null; 
			if (viewModel.ID == 0)
            {
                entity = new Status();
                _Context.AddToStatus(entity);
            }
            else
                entity = _Context.Status.FirstOrDefault(P => P.ID == viewModel.ID);

            entity = Mapper.Map<StatusViewModel, Status>(viewModel, entity);
            _Context.SaveChanges();
            return RedirectToAction("Index");        
        }		
		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
